﻿namespace Yaaf.WP7.NativeAccess.ComWrapper.HTC.COM_FileRw
{
    using System.IO;

    using Microsoft.Phone.InteropServices;

    using System;

    using Yaaf.WP7.NativeAccess.Helper;

    internal class HTCFileAccess : IDisposable
    {
        private IHTCFileAccess htcFileAccess;

        public HTCFileAccess()
        {
            Devices.Initialize();
            FileViewer.Initialize();
            if (ComBridge.RegisterComDll("COM_FileRw.dll", new Guid("EEA7F43B-A32D-4767-9AE7-9E53DA197455")) != 0)
            {
                throw new RegisterComException("Register COM Dll Fail.");
            }
            htcFileAccess = (IHTCFileAccess) new CFileRwClass();
            Init();
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~HTCFileAccess()
        {
            this.Dispose(false);
        }

        private void Dispose(bool disposing)
        {
            this.Deinit();
        }

        public uint CopyFile(string strSource, string strTarget, uint uOverwrite)
        {
            uint num = 0;
            htcFileAccess.CopyFile(strTarget, strSource, uOverwrite, out num);
            //if (num == 2)
            //{
            //    throw new FileNotFoundException("source not found!");
            //}
            return num;
        }

        public uint Deinit()
        {
            uint num = 0;
            htcFileAccess.Deinit(out num);
            return num;
        }

        public uint DeleteFile(string strPath)
        {
            uint num = 0;
            htcFileAccess.DeleteFile(strPath, out num);
            return num;
        }

        public uint GetAllFilesBufferSize(string strPath, string strSearch, out uint uBufLen)
        {
            uint num = 0;
            htcFileAccess.GetAllFileCounts(strPath, strSearch, out uBufLen, out num);
            return num;
        }

        public uint GetAllFileNames(string strPath, string strSearch, byte[] byteBuffer, uint uBufLen)
        {
            uint num = 0;
            htcFileAccess.GetAllFileNames(strPath, strSearch, ref byteBuffer[0], uBufLen, out num);
            return num;
        }

        public uint GetFileSize(string strPath, out uint uFileSize)
        {
            uint num = 0;
            htcFileAccess.GetFileSize(strPath, out uFileSize, out num);
            return num;
        }

        private uint Init()
        {
            uint num = 0;
            htcFileAccess.Init(out num);
            return num;
        }

        public uint GetInformation(string strPath, out uint uFileType)
        {
            uint num = 0;
            htcFileAccess.IsFileExist(strPath, out uFileType, out num);
            return num;
        }

        public uint ReadFile(string strPath, byte[] byteBuffer, uint uBufLen)
        {
            uint num = 0;
            htcFileAccess.ReadFile(strPath, ref byteBuffer[0], uBufLen, out num);
            return num;
        }

        public uint WriteFile(string strPath, byte[] byteBuffer, uint uOverwrite, uint uBufLen)
        {
            uint num = 0;
            htcFileAccess.WriteFile(strPath, ref byteBuffer[0], uBufLen, uOverwrite, out num);
            return num;
        }

    }
}

